package action;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bean.VoteSubject;
import biz.VoteSubjectBiz;
import biz.impl.VoteSubjectBizImpl;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SubjectAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String title;
	private int type;
	ActionContext ac = ActionContext.getContext();
	Map<String, Object> session = ac.getSession();
	private VoteSubjectBiz sb=new VoteSubjectBizImpl();
	private VoteSubject subject=new VoteSubject();
	private Map<Long, Long> votes = new HashMap<Long, Long>();
	
	@SuppressWarnings("unchecked")
	public String list() throws Exception {
		// TODO Auto-generated method stub
		List<VoteSubject> subjectList=sb.findAllSubject();
		List<Object[]> subjectVotes =sb.getUserCountPerSubject(null);
		for (Object[] subjectVote : subjectVotes) {
			votes.put((Long) subjectVote[0], (Long) subjectVote[1]);
		}
		session.put("votes",votes);
		session.put("subjects", subjectList);
		return SUCCESS;
	}
	
	public String add() throws Exception {
		// TODO Auto-generated method stub
		System.out.println(title);
		subject.setVsTitle(title);
		subject.setVsType(type);
		if(sb.save(subject)){
			return "add_success";
		}else{
			return INPUT;
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

}
